import { i18n } from "@/locales/setupI18n";
import { Component } from "vue";
import { Router } from "vue-router";

/*
 * @Author: rxn 1003312535@qq.com
 * @Date: 2024-06-19 13:47:43
 * @LastEditors: rxn 1003312535@qq.com
 * @LastEditTime: 2024-10-10 09:36:30
 * @FilePath: \vue3-progect\src\router\routerHooks.ts
 * @Description: 动态 路由钩子函数
 */
const modules = await import.meta.glob("@/views/**/*.vue");
const layoutMenuls = await import.meta.glob("@/layout/index.vue"); //数据结构
/**
 * /src/views/config/index.vue: () => import("/src/views/config/index.vue")
 */

console.log(modules, "modules");

export const addRoutes = (routerInstance: Router, menuList = []) => {
	if (!menuList.length) return [];
	for (let i = 0; i < menuList.length; i++) {
		console.log("addRoutes.count", i + 1);
		let menu = menuList[i];

		let routeItem = processRoute(menu, false);
		updateLang();
		if (!routerInstance.hasRoute(routeItem.name)) {
			routerInstance.addRoute(routeItem);
		}
		console.log(obj, "obj");
	}
	// let route_404 = processRoute({
	// 	path: "/:pathMatch(.*)*",
	// 	name: "404页面",
	// 	component: "/error/404.vue"
	// });
	// routerInstance.addRoute(route_404);
};
function updateLang() {
	for (let key in obj) {
		i18n.global.mergeLocaleMessage(key, obj[key]);
	}
	console.log(i18n.global.messages, "i18n.global.");
}
interface Mate {
	order: number;
	icon: string;
	title: string;
}

interface RouteItem {
	path: string | "";
	name: string;
	redirect?: string;
	meta?: Mate;
	component?: Component | Promise<Component>;
	children?: RouteItem[];
}
let obj = {};
function processLang(lang, module, isChildren) {
	for (let key in lang) {
		if (!obj[key]) {
			obj[key] = { menu: {} };
		}
		let res = obj[key].menu;

		for (let i = 0; i < module.length; i++) {
			if (res[module[i]]) {
				res = res[module[i]];
			} else {
				if (i < module.length - 1) {
					res[module[i]] = {};
					res = module[i];
				} else {
					if (!isChildren) {
						res[module[i]] = { name: lang[key].content };
						res = res[module[i]];
					} else {
						res[module[i]] = lang[key].content;
					}
				}
			}
		}
	}
}

//格式化-节点
function processRoute(menu, isChildren): RouteItem {
	debugger;
	let module = menu.path.split("/");
	module = module.filter((it) => it);
	processLang(menu.meta.lang || {}, module, isChildren);
	return {
		path: menu.path,
		name: menu.name,
		meta: menu.meta,
		component: formateComponent(menu.component, menu),
		children: formateChildren(menu.children, menu, module)
	};
}
//格式化 -子节点
const formateChildren = (list = [], menu, module) => {
	let result = [];
	if (menu.menuType == "topMenu" || menu.menuType == "module") {
		if (menu.meta.open) {
			result.push(
				processRoute(
					{
						path: "",
						name: menu.name + "Main",
						menuType: "function",
						component: menu.component || menu.path + "/index.vue"
					},
					true
				)
			);
		}
	}
	for (let i = 0; i < list.length; i++) {
		result.push(processRoute(list[i], true));
	}
	return result;
};

//格式化-组件
const formateComponent = (path, menu) => {
	if (menu.menuType == "topMenu") return layoutMenuls[`/src/layout/index.vue`];
	let reg = /\.vue$/gi;
	//组件存在
	if (path) {
		if (reg.test(path)) {
			//有后缀.vue
			return modules[`/src/views${path}`];
		} else if (path == "layout") {
			//layout==//顶部菜单
			return layoutMenuls[`/src/layout/index.vue`];
		} else if (typeof path != "string") {
			return path;
		} else {
			//其他== 没有后缀的
			return modules[`/src/views${path}/index.vue`];
		}
	} else {
		//组件不存在
		return "";
	}
};
